<!-- index-4-1.js -->
//定义全局过滤器
Vue.filter('dateFormat', function(date) {
  var dy = date.getFullYear();
  var dm = date.getMonth() + 1;
  var dd = date.getDate();
  var dh = date.getHours();
  var dms = date.getMinutes();
  var ds = date.getSeconds();
  return `${dy}-${dm}-${dd}  ${dh}:${dms}:${ds}` //正则表达式
  //return dy+"-"+dm+"-"+dd+"  "+dh+":"+dms+":"+ds    //字符串
});
var myViewModel = new Vue({
  el: '#vue41',
  data: {
    // 书籍数量状态
    state:false,
    // 按钮状态
    status:true,
    // 当前的索引
    currIndex:'',
    trClass: 'tr1',
    keyword: '',
    id: '',
    bookname: '',
    press: '',
    author: '',
    bookLists: [{
        id: 1,
        bookname: 'Web前端开发技术',
        press: '清华大学出版社',
        author: '储久良',
        ptime: new Date(),
        operation: ['编辑', '删除']
      },
      {
        id: 2,
        bookname: 'Java程序设计',
        press: '电子工业出版社',
        author: '耿祥义',
        ptime: new Date(),
        operation: ['编辑', '删除']
      },
      {
        id: 3,
        bookname: 'JavaScript高级程序设计',
        press: '高等教育出版社',
        author: '张路路',
        ptime: new Date(),
        operation: ['编辑', '删除']
      }
    ]
  },
  methods: {
    add() {
      //数据不为空，才能添加	
      if (this.id != "" && this.bookname != "" && this.press != "" && this.author != "") {
        if(this.bookLists.find(x=>x.id == this.id)){
          alert("该书id已存在，请选择未被使用的id添加图书")
          return 
        }
        this.bookLists.push({
          id: this.id,
          bookname: this.bookname,
          press: this.press,
          author: this.author,
          ptime: new Date(),
          operation: ['编辑', '删除']
        });
        this.state = false
      } else {
        alert("图书数据不完整！，请补充！！")
      }
      this.id = '';
      this.bookname = '';
    },
    deleteBook(item) {
      this.bookLists = this.bookLists.filter(x=>x.id != item.id)
      if(this.bookLists.length == 0){
          this.state=true
      }else{
        this.state = false
      }
    },
    search(bname) { //返回满足条件的图书数组
      var arrBooks = [];
      if (!bname) {
        return this.bookLists
      } else {
        this.bookLists.forEach((item, i) => {
          if (item.bookname.indexOf(bname) != -1) {
            arrBooks.push(item);
          }
        })
        if (arrBooks.length != 0) {
          
          return arrBooks
        } else {
          alert("查询数据不存在")
          return arrBooks
        }
      }
    },
    // 编辑图书
    editBook(item,i) {
      // 有数据传递过来
      this.status= false
      document.querySelector(".update").style.backgroundColor= 'green'
      // 获取要编辑的当前索引值
      this.currIndex=i
      // 获取到图书信息,并绑定到界面
      this.id = item.id
      this.bookname = item.bookname
      this.press = item.press
      this.author = item.author
    },
    // 更新数据
      update(index) {
        if(this.bookLists[index].id != this.id){
          if(this.bookLists.find(x=>x.id==this.id)){
            alert("用户id已存在，请更换用户id")
            return 
          }
        }
        if(this.bookLists.find(x=>x.id == this.id)){
          this.bookLists[index].id=this.id
          this.bookLists[index].bookname=this.bookname
          this.bookLists[index].press=this.press
          this.bookLists[index].author=this.author
        }else{
          alert("该用户id不存在，无法更新")
          this.id = ""
          this.bookname =""
          this.press = ""
          this.author = ""
          this.status = true
          document.querySelector(".update").style.backgroundColor= 'grey'
        }
        
          
    }
  }
})
